# -*- coding: utf-8 -*-
# Sacado del 4.19 del python cookbook
 ###############################################
 ##                                             ##
 ##   Setup                                      ##
 ##                                              ##
 ##   Taller Básico                              ##
 ##                                              ##
 ##   por Críptidos Digitales                    ##
 ##   GPL (c)2008                                ##
  ##                                             ##
    ###############################################

import os, fnmatch
from distutils.core import setup
import py2exe

import TB

def listFiles(root, patterns='*', recurse=0, return_folders=0):
    """ Expand patterns from semicolon-separated string to list """
    pattern_list = patterns.split(';')

    """ Collect input and output arguments into one bunch """
    class Bunch:
        def __init__(self, **kwds):
            self.__dict__.update(kwds)

    arg = Bunch(recurse=recurse, pattern_list=pattern_list, return_folders=return_folders, results=[])

    def visit(arg, dirname, files):
        """ Append to arg.results all relevant files (and perhaps folders) """
        for name in files:
            fullname = os.path.normpath(os.path.join(dirname, name))
            if arg.return_folders or os.path.isfile(fullname):
                for pattern in arg.pattern_list:
                    if fnmatch.fnmatch(name, pattern):
                        arg.results.append(fullname)
                        break

        """ Block recursion if recursion was disallowed """
        if not arg.recurse:
            files[:] = []

    os.path.walk(root, visit, arg)
    return arg.results


def filterFiles(root, patterns='*', recurse=0, return_folders=0):
    archivos = listFiles(root, patterns, recurse, return_folders)
    filtered = []
    estosDirectoriosNo = ('CVS', 'DIST', 'BUILD', 'ATICO', 'RESOURCES', '.svn')
    for archivo in archivos:
        encontrado = False
        for directorio in estosDirectoriosNo:
            if directorio in archivo.upper():
                encontrado = True
        if not encontrado:
            filtered.append(archivo)
    return filtered


app = TB.MyApp([])


setup(name = "Taller Basico",
        version = "%s" % app.info['version'],
        windows = ["TB.py",
            { "icon_resources": [(1, "imagenes/icon.ico")],
              "script": "TB.py"}
        ],
        data_files=[
            ('imagenes', filterFiles('imagenes', '*.png;*.jpg;*.gif;*.ico')),
            ('propio', filterFiles('propio', '*.txt;*.jpg')),
            ('documentos', filterFiles('documentos', '*.*')),
            ('', ('changelog.txt', ))
        ],
        options={
            "py2exe":{
                "packages":["encodings", "sqlalchemy", "psycopg2"],
                "includes":["sip"],
                "excludes":["_ssl", "pyreadline", "difflib", "doctest", "optparse", "pickle", "calendar", "kinterbasdb"]
                    }
            }
    )


# uso : python setup.py py2exe

